package com.gitee.zycra.union.api.manager;

import com.gitee.zycra.union.api.common.model.ApiMockInstance;
import com.gitee.zycra.union.api.common.model.ApiMockInstanceCase;
import com.gitee.zycra.union.api.common.param.AddMockInstanceParam;
import com.gitee.zycra.union.api.common.param.UpdateInstanceCaseParam;
import com.gitee.zycra.union.api.common.util.IdGenerator;
import com.gitee.zycra.union.api.common.vo.InstanceCaseVO;
import com.gitee.zycra.union.api.common.vo.InstanceVO;
import com.gitee.zycra.union.api.dao.ApiMockInstanceCaseDao;
import com.gitee.zycra.union.api.dao.ApiMockInstanceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zycra
 */
@Component
public class ApiMockInstanceManager {

    @Autowired
    private ApiMockInstanceDao apiMockInstanceDao;

    @Autowired
    private ApiMockInstanceCaseDao apiMockInstanceCaseDao;

    @Autowired
    private IdGenerator idGenerator;

    /**
     * 添加mock实例
     *
     * @param addMockInstanceParam mock实例
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int addMockInstance(AddMockInstanceParam addMockInstanceParam) {
        Long instanceId = idGenerator.nextId();
        int result = apiMockInstanceDao.insert(new ApiMockInstance().setId(instanceId)
                .setTemplateId(addMockInstanceParam.getTemplateId())
                .setInstanceName(addMockInstanceParam.getInstanceName()));
        apiMockInstanceCaseDao.batchInsert(AddMockInstanceParam.convertToApiMockInstanceCaseList(addMockInstanceParam.getCaseList(), instanceId));
        return result;
    }

    /**
     * 更新mock实例
     *
     * @param id           实例ID
     * @param instanceName 实例名称
     * @return 影响行数
     */
    public int updateMockInstance(Long id, String instanceName) {
        return apiMockInstanceDao.updateById(new ApiMockInstance().setId(id).setInstanceName(instanceName));
    }

    /**
     * 删除nock实例
     *
     * @param id 实例ID
     * @return 影响行数
     */
    public int deleteMockInstance(Long id) {
        return apiMockInstanceDao.updateToDelete(id);
    }

    /**
     * 查询mock实例
     *
     * @param templateId 模板ID
     * @return 查询结果
     */
    public List<InstanceVO> selectMockInstance(Long templateId) {
        return apiMockInstanceDao.selectMockInstance(templateId);
    }

    /**
     * 根据实例ID查询实例期望
     *
     * @param instanceId 模板ID
     * @return 查询结果
     */
    public List<InstanceCaseVO> selectMockInstanceCase(Long instanceId) {
        return apiMockInstanceCaseDao.selectMockInstanceCase(instanceId);
    }

    /**
     * 更新实例期望配置
     *
     * @param updateInstanceCaseParam 更新参数
     * @return 影响行数
     */
    public int updateMockInstanceCase(UpdateInstanceCaseParam updateInstanceCaseParam) {
        ApiMockInstanceCase apiMockInstanceCase = apiMockInstanceCaseDao.selectByInstanceAndInterfaceId(updateInstanceCaseParam.getInstanceId(),
                updateInstanceCaseParam.getInterfaceId());
        if (apiMockInstanceCase != null) {
            return apiMockInstanceCaseDao.updateById(new ApiMockInstanceCase()
                    .setInstanceId(apiMockInstanceCase.getId()).setCaseId(updateInstanceCaseParam.getCaseId()));
        }
        apiMockInstanceCase = UpdateInstanceCaseParam.convertToApiMockInstanceCase(updateInstanceCaseParam);
        apiMockInstanceCase.setId(idGenerator.nextId());
        return apiMockInstanceCaseDao.insert(apiMockInstanceCase);
    }
}
